package com.jxc.dao.impl;

import com.jxc.dao.GoodsRepertoryDaoI;
import com.jxc.model.po.Brand;
import com.jxc.model.po.Goods;
import com.jxc.model.po.GoodsRepertory;
import com.jxc.model.po.GoodsRepertory;
import com.jxc.model.po.Repertory;
import com.jxc.model.po.User;
import com.jxc.model.vo.VGoodsRepertory;
import com.jxc.model.vo.VRemindWarn;

import java.io.Serializable;
import java.util.*;

import org.hibernate.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository("threadGoodsRepertoryDao")
public class GoodsRepertoryDaoImpl implements GoodsRepertoryDaoI
{

	@Autowired
	private SessionFactory sessionFactory;

	@Override
	public Serializable save(GoodsRepertory goodsRepertory) {
		Session session = this.sessionFactory.openSession();
		Serializable serial = session.save(goodsRepertory);
		session.close();
		return serial;
	}
	
	@Override
	public void delete(GoodsRepertory goodsRepertory) {
		Session session = this.sessionFactory.openSession();
		session.beginTransaction();
		session.delete(goodsRepertory);
		session.getTransaction().commit();
		session.close();
	}
	
	@Override
	public List<VGoodsRepertory> combobox(VGoodsRepertory vGoodsRepertory) {
		Session session = this.sessionFactory.openSession();
		List<VGoodsRepertory> list = this.changeModelGoodsRepertory(this.find(vGoodsRepertory,session));
		session.close();
		return list;
	}

	private List<GoodsRepertory> find(VGoodsRepertory vGoodsRepertory,Session session) {
		String hql = "from GoodsRepertory t where 1=1 ";

		List<Object> values = new ArrayList<Object>();

		Query q = session.createQuery(hql);
		if (values != null && values.size() > 0) {
			for (int i = 0; i < values.size(); i++) {
				q.setParameter(i, values.get(i));
			}
		}

		return q.list();
	}

	private List<VGoodsRepertory> changeModelGoodsRepertory(List<GoodsRepertory> goodsRepertorys) {
		List<VGoodsRepertory> vGoodsRepertorys = new ArrayList<VGoodsRepertory>();
		if (goodsRepertorys != null && goodsRepertorys.size() > 0) {
			for (GoodsRepertory gr : goodsRepertorys) {
				VGoodsRepertory vGoodsRepertory = new VGoodsRepertory();
				BeanUtils.copyProperties(gr, vGoodsRepertory);
				if(gr.getGoods()!=null){
					Goods g = gr.getGoods();
					if(g.getId()!=null){
						vGoodsRepertory.setGid(g.getId());
					}
					if(g.getName()!=null){
						vGoodsRepertory.setGname(g.getName());
					}
					if(g.getType()!=null){
						vGoodsRepertory.setType(g.getType());
					}
					if(g.getSize()!=null){
						vGoodsRepertory.setSize(g.getSize());
					}
					if(g.getBrand()!=null){
						Brand b = g.getBrand();
						if(b.getId()!=null){
							vGoodsRepertory.setBid(b.getId());
						}
						if(b.getName()!=null){
							vGoodsRepertory.setBname(b.getName());
						}
					}
				}
				if(gr.getRepertory()!=null){
					Repertory r = gr.getRepertory();
					if(r.getId()!=null){
						vGoodsRepertory.setRid(r.getId());
					}
					if(r.getName()!=null){
						vGoodsRepertory.setRname(r.getName());
					}
					if(r.getUser()!=null){
						User u = r.getUser();
						if(u.getId()!=null){
							vGoodsRepertory.setUid(u.getId());
						}
						if(u.getUsername()!=null){
							vGoodsRepertory.setUname(u.getUsername());
						}
					}
				}
				vGoodsRepertorys.add(vGoodsRepertory);
			}
		}
		return vGoodsRepertorys;
	}

	

}